<?php


class LoginApp extends MallbaseApp
{

    function index()
    {
        $hist = trim($_REQUEST['hist']);
        //$wxNum = empty($_REQUEST['wxNum']) ? 'wx123' : trim($_REQUEST['wxNum']);
        if (!IS_POST) {
            $this->display('login.html');
        } else {
            $userName = empty($_POST['userName']) ? '' : trim($_POST['userName']);
            $userPwd = empty($_POST['userPwd']) ? '' : trim($_POST['userPwd']);
            $wxNum = isset($_COOKIE['wxid'])?$_COOKIE['wxid']:'';
            if(empty($wxNum)){
                echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                echo "<script>alert('授权未成功');  window.location.href='index.php?app=login'; </script>  ";//使用weixin.php的页面
            }
            if ($this->checkUrl(WEB_SERVICE)) {
                $client = new soapclient(WEB_SERVICE);
                $client->soap_defencoding = 'utf-8';
                $client->decode_utf8 = false;
                $client->xml_encoding = 'utf-8';
                $param = array('json' => '{"userName":"' . $userName . '","userPwd":"' . $userPwd . '","wxNum": "' . $wxNum . '"}');
                $result = $client->__Call("UpdWinXin", array($param));
                if (is_soap_fault($result)) {
                    trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
                } else {
                    $result = json_decode(json_encode($result->UpdWinXinResult), TRUE);
                    $result = json_decode($result, true);

                    if ($result['status'] == 1) {
                        $app = $hist ? $hist : 'login';
                        $app == 'login' and $app = 'menu';
                        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                        echo "<script>alert('绑定微信成功'); window.location.href='index.php?app=$app';</script>  ";
                    } elseif (strstr($result['result'], '该用户无权')) {
                        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                        echo "<script>alert('$result[result]'); window.location.href='index.php?app=menu'; </script>  ";
                        exit();
                    } else {
                        $msg = $result['result'];
                        echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
                        echo "<script>alert('" . $msg . "');  window.location.href='index.php?app=login'; </script>  ";
                    }

                }


            }

        }


    }


}